package com.nchu.controller;

import com.nchu.mappers.EvaluationMapper;
import com.nchu.mappers.UserMapper;
import com.nchu.pojo.OrderComplain;
import com.nchu.pojo.OrderEvaluation;
import com.nchu.pojo.User;
import com.nchu.pojo.UserAddress;
import com.nchu.service.EvaluationService;
import com.nchu.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Date:2023-07-11 8:14
 * Author：whx
 */
@Controller
@RequestMapping("/user")
public class UserController {
//    @Autowired
//    private UserService service;
    @Autowired
   private EvaluationService EvaluationService;

    @Autowired
    private UserService service;


    @RequestMapping("/login")
    public String login()
    {
        return "login";
    }
    @RequestMapping("/get_userInfo")
    @ResponseBody
    public User getUserInfo(@RequestParam int UserID){
        System.out.println("test /user/get_userInfo"+UserID);
        System.out.println(service.userisexist(UserID));
        return service.userisexist(UserID);
    }

    @RequestMapping("/check")
    public String check(int id, String pwd, HttpSession session,Model model)
    {
        User user =service.login(id, pwd);
        if(user==null)
        {
            model.addAttribute("msg"," 账号或密码错误");
//            model.a("passwordError", true);
//            return "redirect:/user/login";
            return "login";
        }
        else
        {
            model.addAttribute("msg",null);
            session.setAttribute("user",user);
            return "homepage";
        }
    }
    @RequestMapping("/quit")
    public String quit(Model model,HttpSession session)
    {
        session.invalidate();
        return "login";
    }
    @RequestMapping("/money")
    public String money(Model model,HttpSession session){
        return "money";
    }
    @RequestMapping("/account")
    public String account(Model model,HttpSession session){
        User user = (User) session.getAttribute("user");
        int userid = user.getUserId();
        service.userisexist(userid);

        model.addAttribute("user",user);

        return "account";
    }
    @RequestMapping("/homepage")
    public String homePage(Model model,HttpSession session){
        return "homepage";
    }
    @RequestMapping("/evaluate")
    public String evaluate(Model model,HttpSession session){
        User user = (User) session.getAttribute("user");
        List<OrderEvaluation> orderformme = EvaluationService.orderformme(user.getUserId());
        List<OrderEvaluation> ordertome = EvaluationService.ordertome(user.getUserId());
        model.addAttribute("ordertome",ordertome);
        model.addAttribute("orderformme",orderformme);
        orderformme.stream().forEach(System.out::println);
        return "evaluate";
    }
    @RequestMapping("/ChangePassword")
    public String ChangePassword(Model model,HttpSession session){
        return "ChangePassword";
    }
    @RequestMapping("/mypage")
    public String myPage(Model model,HttpSession session)
    {
        User user = (User) session.getAttribute("user");
        model.addAttribute("user",user);
        return "mypage";
    }
    @RequestMapping("/run_pool")
    public String runPool(Model model,HttpSession session)
    {
        return "redirect:/order/display_orders";
    }

    @RequestMapping("req")
    public String req(int userid,String username, String pwd1, String pwd2, String phone,
                      String vcade, Model model)
    {

        User user=new User();
        user.setUserId(userid);
        User useris =service.userisexist(user.getUserId());
        if(useris==null)
        {
            user.setUserName(username);
            user.setPhoneNumber(phone);
            user.setPwd(pwd1);
            user.setStatus(0);
            model.addAttribute("user",user);
            if (!pwd1.equals(pwd2)){
                model.addAttribute("msg","两次密码不一致");
                return "login";
            }
            service.register(user);
            model.addAttribute("msg",null);
            return "login";
        }
        else
        {
            model.addAttribute("msg","用户已经注册");
            return "login";
        }

    }

    @RequestMapping("changepwd")
    public String req(String pwd1, String pwd2, String pwd3, Model model,HttpSession session){
        User user = (User) session.getAttribute("user");
        if(pwd1.equals(user.getPwd())){
            if (!pwd2.equals(pwd3)){
                model.addAttribute("msg","两次密码不一致");
                return "ChangePassword";
            }
            else {
                service.changepwd(user.getUserId(),pwd3);
            }
        }
        else {
            model.addAttribute("msg","原始密码不正确");
            return "ChangePassword";
        }
        model.addAttribute("user",user);
        return "mypage";
    }
    @RequestMapping("/updatauser")
    public String updatauser(int userid,String username,String phone, Model model,HttpSession session){
        System.out.println(userid);
        System.out.println(username);
        System.out.println(phone);
        User user = (User) session.getAttribute("user");
        service.UpdateUserById(userid,username,phone);
        user.setUserName(username);
        user.setPhoneNumber(phone);
        session.setAttribute("user",user);
        model.addAttribute("user",user);
        return "mypage";
    }


    @GetMapping("/address")
    public String address(@RequestParam("userid") int userid,Model model,HttpSession session)
    {
        User user = (User) session.getAttribute("user");
        model.addAttribute("user",user);
        List<UserAddress> addressList =service.FindAddressbyid(userid);
        addressList.stream().forEach(System.out::println);
        model.addAttribute("addressList",addressList);
        return "address";
    }

    @GetMapping("/defaultaddress")
    public String defaultaddress(@RequestParam("serialnumber") int serialnumber,@RequestParam("userid") int userid,Model model,HttpSession session)
    {
        service.changeaddress0(userid);
        service.changeaddress1(serialnumber);
        User user = (User) session.getAttribute("user");
        model.addAttribute("user",user);
        List<UserAddress> addressList =service.FindAddressbyid(userid);
        model.addAttribute("addressList",addressList);
        return "address";
    }
    @GetMapping("/editaddress")
    public String editaddress(@RequestParam("serialnumber") int serialnumber,Model model,HttpSession session)
    {
        User user = (User) session.getAttribute("user");
        model.addAttribute("user",user);
        UserAddress address = service.address(serialnumber);
        model.addAttribute("address",address);
        model.addAttribute("serialnumber",serialnumber);
        return "edit_address";
    }

    @RequestMapping("/updateaddress")
    public String updateaddress(int serialnumber,String name,String phone,String address,Model model,HttpSession session)
    {
        service.updateaddress(serialnumber,name,address,phone);
        User user = (User) session.getAttribute("user");
        model.addAttribute("user",user);
        List<UserAddress> addressList =service.FindAddressbyid(user.getUserId());
        model.addAttribute("addressList",addressList);
        return "address";
    }


    @GetMapping("/deleteaddress")
    public String deleteaddress(@RequestParam("serialnumber") int serialnumber,Model model,HttpSession session)
    {
        service.deleteaddress(serialnumber);
        User user = (User) session.getAttribute("user");
        model.addAttribute("user",user);
        List<UserAddress> addressList =service.FindAddressbyid(user.getUserId());
        model.addAttribute("addressList",addressList);

        return "address";
    }

    @RequestMapping("/addaddress")
    public String addaddress(Model model,HttpSession session)
    {
        User user = (User) session.getAttribute("user");
        model.addAttribute("user",user);
        return "add_address";
    }

    @RequestMapping("/addressadd")
    public String addressadd(int userid,String name,String address,String phone,Model model,HttpSession session)
    {
        service.addaddress(userid,name,address,phone);
        User user = (User) session.getAttribute("user");
        model.addAttribute("user",user);
        List<UserAddress> addressList =service.FindAddressbyid(user.getUserId());
        model.addAttribute("addressList",addressList);
        return "address";
    }



    @RequestMapping("mycomplaints")
    public String mycomplaints(Model model,HttpSession session)
    {
        User user = (User) session.getAttribute("user");
        int userid = user.getUserId();
        List<OrderComplain> orderComplainList = service.findallcomplainbyid(userid);
        model.addAttribute("orderComplainList",orderComplainList);
        return "mycomplaints";
    }






}
